package fr.istic.linkedcv.client.presentation.views.impl;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.istic.linkedcv.client.PersonneCli;
import fr.istic.linkedcv.client.presentation.Personne;
import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.services.PersonneService;
import fr.istic.linkedcv.client.services.PersonneServiceAsync;

public class ListeAmis extends View {
	private VerticalPanel vPanel;
	private final PersonneServiceAsync personneServiceAsync = GWT.create(PersonneService.class);
	public ListeAmis(Ui ui) {
		super(ui);
	}
	@Override
	public void build() {
		vPanel = new VerticalPanel();
		vPanel.addStyleName("lists");
		vPanel.setWidth("370px");
		personneServiceAsync.getFriends(new AsyncCallback<List<PersonneCli>>() {
			
			@Override
			public void onSuccess(List<PersonneCli> result) {
				if(result.size()>0){
					for(PersonneCli personne : result){
						vPanel.add(new Personne(personne, ui));					
					}
				
					Button sendMessageBtn = new Button("Envoyer un message à tous les amis");
					sendMessageBtn.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							ui.addHistoryItem("message_form");							
						}
					});
					vPanel.add(sendMessageBtn);
				}else{
					vPanel.add(new HTML("Aucun ami"));
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
			}
		});
		
	}

	@Override
	public Widget getWidget() {
		return vPanel;
	}

}
